package com.twetec.cms.adapter.pc.backend.tag;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Maps;
import com.twetec.cms.adapter.pc.backend.tag.annotation.TplTag;
import com.twetec.cms.adapter.pc.backend.tag.doc.TagDocObject;
import com.twetec.cms.common.util.validate.ValidateUtil;
import com.twetec.cms.model.po.website.ArticleAttachmentPO;
import com.twetec.cms.model.po.website.ArticlePO;
import com.twetec.cms.model.po.website.ArticleRelevantPO;
import com.twetec.cms.model.po.website.ColumnPO;
import com.twetec.cms.service.website.*;
import org.beetl.core.tag.GeneralVarTagBinding;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service()
@Scope("prototype")
@TplTag("articleRelevantList")
public class ArticleRelevantListListTag extends GeneralVarTagBinding {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private ColumnService columnService;

    @Autowired
    private ArticleRelevantService relevantService;

    @Override
    public void render() {
        System.out.println("ArticleRelevantList");

        // 文章id
        Long articleId = (this.getAttributeValue("articleId") instanceof String) ?
                Long.parseLong((String) this.getAttributeValue("articleId")) : (Long) this.getAttributeValue("articleId");
//        String dm = (String) this.getAttributeValue("dm");

//        String serverUrl = (String) this.getAttributeValue("serverUrl");

//        String pageType = (String) this.getAttributeValue("pageType");


        // 相关文章(xxxx:[],[],[]) {"relevantArticleId":"1447128310213885953","relevantArticleId":"1447128364974718978"}
        QueryWrapper<ArticleRelevantPO> articleRelevantPOQueryWrapper = new QueryWrapper<>();
        articleRelevantPOQueryWrapper.eq("ARTICLE_ID", articleId).select("ARTICLE_ID", "RELEVANT_ARTICLE_ID");
        List<ArticleRelevantPO> relevantPOList = relevantService.list(articleRelevantPOQueryWrapper);

        if (!ValidateUtil.isNullOrEmpty(relevantPOList)) {
            for (ArticleRelevantPO relevantPO : relevantPOList) {

                // 查询文章信息
                QueryWrapper<ArticlePO> articlePOQueryWrapper = new QueryWrapper<>();
                articlePOQueryWrapper.select("TITLE", "ID", "COLUMN_ID").eq("ID", relevantPO.getRelevantArticleId());
                ArticlePO articlePO = articleService.getOne(articlePOQueryWrapper);
                if (null == articlePO) continue;

                // 查询所属栏目的encodedXpath
                // 查询文章信息
                ColumnPO columnPO = columnService.getById(articlePO.getColumnId());

                // 域名
//                String url = dm + columnPO.getEncodedXpath() + "/article/" + articlePO.getId() + ".html";
                String url = columnPO.getEncodedXpath() + "/article/" + articlePO.getId() + ".html";

                Map<String, String> map = new HashMap<>();
                map.put("title", articlePO.getTitle());
//                if (null != pageType && pageType.equals("checkPage")) {
//                    map.put("url", serverUrl + "website/article/get-pc-template-render-content?articleId=" + articlePO.getId());
//                } else {
                    map.put("url", url);
//                }
                this.binds(map);
                this.doBodyRender();
            }

        }
    }


    /**
     * 标签文档
     *
     * @return
     */
    public static TagDocObject doc() {
        TagDocObject tagDocObject = new TagDocObject();
        tagDocObject.setTagName("articleRelevantList");
        tagDocObject.setDesc("文章相关文章列表标签");
        tagDocObject.setExample("<#articleRelevantList serverUrl=\"${serverUrl}\" pageType=\"${pageType}\" dm=\"${dm}\" articleId=\"${data.articleId!}\" var=\"data\">" +
                "                          <li class=\"news-item\">\n" +
                "                              <a href=\"${data.url!}\">\n" +
                "                                  <span class=\"cricle-li\"></span>\n" +
                "                                  <span class=\"new-inner\">${data.title!}</span>\n" +
                "                              </a>\n" +
                "                          </li>\n" +
                "                      </#articleRelevantList>");

        tagDocObject.setParamDesc("articleId：文章id\n" +
                "dm：文章地址\n" +
                "serverUrl：资讯发布系统的域名\n" +
                "pageType：用于标识是生成静态页面还是预览（预览：checkPage，生成：null或staticPage）\n" +
                "--------- var: 标签的数据结果集 ---------\n" +
                "title：相关文章标题\n" +
                "url：文章链接\n");

        tagDocObject.setOtherDesc("无");

        return tagDocObject;
    }

}
